package com.zyg.zlib.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zyg
 * 依赖glide加载gif
 * 多个View公用一个Lable时，需要调用绑定View
 */
public class GifLableDrawable extends LableDrawable implements Drawable.Callback {

    private List<WeakReference<View>> refreshListeners;

    private GifDrawable gifDrawable;

    /**
     * 单个view播放
     */
    private boolean singleView;

    private GifLableDrawable(Builder builder) {
        super(builder);
        this.singleView = builder.singleView;
    }

    /**
     * Add refresh listener.
     *
     * @param view the view
     */
    public void bindView(View view) {
        if (refreshListeners == null) {
            refreshListeners = new ArrayList<>();
            refreshListeners.add(new WeakReference<>(view));
            return;
        }
        for (WeakReference<View> refreshListener : refreshListeners) {
            if (refreshListener != null && refreshListener.get() != null) {
                if (refreshListener.get().equals(view)) {
                    return;
                }
            }
        }
        refreshListeners.add(new WeakReference<>(view));
    }

    @Override
    public void invalidateDrawable(@NonNull Drawable who) {
        if (singleView) {
            final Callback callback = getCallback();
            if (callback != null) {
                callback.invalidateDrawable(this);
            }
        } else {
            for (WeakReference<View> refreshListener : refreshListeners) {
                if (refreshListener != null && refreshListener.get() != null) {
                    refreshListener.get().invalidate();
                }
            }
        }
    }

    @Override
    public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {

    }

    @Override
    public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {

    }


    /**
     * Start.
     */
    public void start() {
        if (gifDrawable != null) {
            gifDrawable.setLoopCount(GifDrawable.LOOP_FOREVER);
            gifDrawable.start();
        }
    }

    /**
     * Stop.
     */
    public void stop() {
        if (gifDrawable != null) {
            gifDrawable.setLoopCount(GifDrawable.LOOP_FOREVER);
            gifDrawable.stop();
        }
    }


    public static class Builder extends LableBuilder<GifLableDrawable.Builder, GifLableDrawable> {

        private int iconRes;
        private boolean singleView;

        public static Builder newBuilder(Context context){
            return new Builder(context);
        }

        Builder(Context context) {
            super(context);
        }

        public Builder icon(@DrawableRes int iconRes) {
            super.icon(iconRes);
            this.iconRes = iconRes;
            return this;
        }

        public Builder singleView(boolean singleView) {
            this.singleView = singleView;
            return this;
        }


        @Override
        public GifLableDrawable build() {
            final GifLableDrawable gifLableDrawable = new GifLableDrawable(this);
            Glide.with(context)
                    .load(iconRes)
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            if (resource instanceof GifDrawable) {
                                GifDrawable drawable = (GifDrawable) resource;
                                drawable.setCallback(gifLableDrawable);
                                drawable.setBounds(gifLableDrawable.icon.getBounds());
                                gifLableDrawable.icon = drawable;
                                gifLableDrawable.gifDrawable = drawable;
                                drawable.setLoopCount(GifDrawable.LOOP_FOREVER);
                                drawable.start();
                            }
                        }
                    });
            return gifLableDrawable;
        }
    }

}
